package com.tools.cleanmaster.ui.card;

import android.app.Activity;
import android.view.View;
import android.widget.Toast;

import com.tools.ad.EntranceType;
import com.tools.ad.util.GARecordUtils;
import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.CleanGAConstant;
import com.tools.cleanmaster.R;
import com.tools.cleanmaster.bean.AutoCleanFeature;
import com.tools.cleanmaster.ui.card.factory.CardListAdapter;
import com.tools.cleanmaster.ui.card.viewholder.ACardViewHolder;
import com.tools.cleanmaster.ui.card.viewholder.CommonCardViewHolder;
import com.tools.cleanmaster.ui.view.ToastHelper;

import java.util.Locale;

/**
 * Created by wuyanfei on 16/4/25.
 */
public class AutoCleanCardItem extends ACardItem {
    private CommonCardViewHolder mViewHolder;

    public AutoCleanCardItem(EntranceType entranceType, int pos) {
        super(entranceType, pos);
    }

    @Override
    public CardViewType getCardViewType() {
        return CardViewType.AUTO_CLEAN;
    }

    @Override
    public boolean needShow(EntranceType entranceType) {
        return !AutoCleanFeature.AUTO_CACHE.isEnable() && !AutoCleanFeature.AUTO_MEMORY.isEnable();
    }

    @Override
    public String getCardName() {
        return CardType.AUTO_CLEAN.key;
    }

    @Override
    public void bindCardView(final Activity activity, ACardViewHolder viewHolder, final CardListAdapter cardAdapter, final int position) {
        super.bindCardView(activity, viewHolder, cardAdapter, position);

        mViewHolder = (CommonCardViewHolder) viewHolder;
        mViewHolder.mLogo.setImageResource(R.drawable.card_func_auto);
        mViewHolder.mSmallLogo.setVisibility(View.GONE);
        mViewHolder.mTitle.setText(activity.getString(R.string.item_card_smart_clean_title));
        mViewHolder.mDesc.setText(activity.getString(R.string.item_card_smart_clean_desc));
        mViewHolder.mExecute.setText(R.string.screen_card_btn_txt);

        mViewHolder.mAdCorner.setVisibility(View.GONE);
        mViewHolder.mItemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClick(activity, cardAdapter, position);
            }
        });
    }

    private void doClick(Activity activity, CardListAdapter adapter, int position) {
        imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.AUTO_CACHE, true);
        imContext.getAutoCleanManager().setFeatureEnable(AutoCleanFeature.AUTO_MEMORY, true);

        ToastHelper.create(CleanApplication.getInstance(), activity.getString(R.string.auto_clean_enable_toast), Toast.LENGTH_LONG).show();
        adapter.dismissAnimation(position);

        if (mViewHolder.mPageEntrance == EntranceType.HOME_TRASH_COMPLETED && imContext.getCleanRawCache().isFirstEnterApplication()) {
            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    String.format(Locale.ENGLISH, CleanGAConstant.L_CARD_SMART_CLEAN_FIRST, String.valueOf(mViewHolder.mPageEntrance.getName())));
        } else {
            GARecordUtils.report(imContext, CleanGAConstant.C_FUNC, CleanGAConstant.A_CLICK,
                    String.format(Locale.ENGLISH, CleanGAConstant.L_CARD_SMART_CLEAN, String.valueOf(mViewHolder.mPageEntrance.getName())));
        }
    }

    @Override
    public void onActivityDestroy() {
        super.onActivityDestroy();
    }
}
